package data;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

public class EVEItem {

  private String                                    _itemPrimaryKey;
  private String                                    _itemID;
  private String                                    _itemName;
  private HashMap<EVESystem, EVECentralMarketOrder> _itemMarketDataMap;

  public EVEItem(String pk, String id, String name) throws IOException {
    _itemPrimaryKey = pk;
    _itemID = id;
    _itemName = name;
    _itemMarketDataMap = new HashMap<EVESystem, EVECentralMarketOrder>();
  }

  public void dump() {
    System.out.println("PK = " + _itemPrimaryKey);
    System.out.println("ID = " + _itemID);
    System.out.println("Name = " + _itemName);
  }

  // public void _dump() {
  // for (Iterator<EVESystem> IT = _itemMarketDataMap.keySet().iterator();
  // IT.hasNext();) {
  // EVESystem key = (EVESystem) IT.next();
  // System.out.println("Dump for System:" + key);
  // EVECentralMarketOrder order = _itemMarketDataMap.get(key);
  // order._dump();
  // }
  // }

  public void _addSystem(EVESystem system) throws IOException {
    EVECentralMarketOrder order = new EVECentralMarketOrder();
    _itemMarketDataMap.put(system, order);
    system.getSystemMarketDataMap().put(this, order);
  }

  public void _removeSystem(EVESystem eveSystem) {
    _itemMarketDataMap.remove(eveSystem);
  }

  public String _dumpToString() {
    String dumpString = _itemName;
    for (Iterator<EVESystem> IT = _itemMarketDataMap.keySet().iterator(); IT.hasNext();) {
      EVESystem key = (EVESystem) IT.next();
      EVECentralMarketOrder order = _itemMarketDataMap.get(key);
      dumpString += "\n" + key + "\n" + order._dumpToString();
    }
    return dumpString;
  }

  @Override
  public String toString() {
    return _itemName;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((_itemPrimaryKey == null) ? 0 : _itemPrimaryKey.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;
    if (getClass() != obj.getClass()) return false;
    EVEItem other = (EVEItem) obj;
    if (_itemPrimaryKey == null) {
      if (other._itemPrimaryKey != null) return false;
    }
    else if (!_itemPrimaryKey.equals(other._itemPrimaryKey)) return false;
    return true;
  }

  public String getItemName() {
    return _itemName;
  }

  public HashMap<EVESystem, EVECentralMarketOrder> getItemMarketDataMap() {
    return _itemMarketDataMap;
  }

  public String getItemPrimaryKey() {
    return _itemPrimaryKey;
  }

  public String getItemID() {
    return _itemID;
  }
}
